package meetingcostclock;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SettingsDialog {
	
	private SettingsProvider settings;
	
	public SettingsDialog() {
		settings = new SettingsProvider();
		
		final Display display = new Display();
		final Shell shell = new Shell(display, SWT.CLOSE|SWT.TITLE|SWT.TOOL);
		FormLayout layout = new FormLayout ();
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		layout.spacing = 10;
		shell.setLayout(layout);
		shell.setSize(330, 200);
		shell.setText("MeetingCostClock Settings");

		final Text valueNoOfPart = new Text(shell, SWT.BORDER);
		final Text valueRate = new Text(shell, SWT.BORDER);
		final Combo valueLocale = new Combo(shell, SWT.READ_ONLY);
		final Locale[] locales = Locale.getAvailableLocales();
		Arrays.sort(locales, new LocaleComparator());
		final Label labelMessage = new Label(shell, SWT.NONE);
		
		// no of part
		Integer noOfPart = settings.getNoOfParticipants();
		Label labelNoOfPart = new Label(shell, SWT.NONE);
		labelNoOfPart.setText("Number of participants: ");
		FormData lData = new FormData();
		if (noOfPart != null) valueNoOfPart.setText(noOfPart.toString());
		lData = new FormData();
		lData.left = new FormAttachment (40, 0);
		lData.top = new FormAttachment (labelNoOfPart, 0, SWT.CENTER);
		lData.width = 75;
		valueNoOfPart.setLayoutData(lData);

		// rate
		Integer rate = settings.getRate();
		Label labelRate = new Label(shell, SWT.NONE);
		labelRate.setText("Hourly rate: ");
		lData = new FormData();
		lData.top = new FormAttachment (labelNoOfPart, 3, SWT.LEFT);
		labelRate.setLayoutData(lData);
		if (rate != null) valueRate.setText(rate.toString());
		lData = new FormData();
		lData.left = new FormAttachment (40, 0);
		lData.top = new FormAttachment (labelRate, 0, SWT.CENTER);
		lData.width = 75;
		valueRate.setLayoutData(lData);

		// locale
		//Integer rate = settings.getRate();
		Label labelLocale = new Label(shell, SWT.NONE);
		labelLocale.setText("Locale: ");
		lData = new FormData();
		lData.top = new FormAttachment (labelRate, 3, SWT.LEFT);
		labelLocale.setLayoutData(lData);
		valueLocale.setVisibleItemCount(15);
		for (Locale locale : locales) {
			valueLocale.add(locale.getDisplayName());
		}
		valueLocale.setText(settings.getLocale().getDisplayName());
		lData = new FormData();
		lData.left = new FormAttachment (40, 0);
		lData.top = new FormAttachment (labelLocale, 0, SWT.CENTER);
		lData.width = 150;
		valueLocale.setLayoutData(lData);

		// on top?
		boolean onTop = settings.getAlwaysOnTop();
		Label labelDisplay = new Label(shell, SWT.NONE);
		labelDisplay.setText("Display: ");
		lData = new FormData();
		lData.top = new FormAttachment (labelLocale, 3, SWT.LEFT);
		labelDisplay.setLayoutData(lData);
		final Button checkOnTop = new Button(shell, SWT.CHECK);
		checkOnTop.setText("Always on top");
		checkOnTop.setSelection(onTop);
		lData = new FormData();
		lData.left = new FormAttachment (40, 0);
		lData.top = new FormAttachment (labelDisplay, 0, SWT.CENTER);
		checkOnTop.setLayoutData(lData);

		
		labelMessage.setForeground(new Color(display, 255,0,0));
		lData = new FormData();
		lData.top = new FormAttachment (labelDisplay, 5, SWT.LEFT);
		lData.width = 220;
		labelMessage.setLayoutData(lData);

		// ok and cancel
		Button btnCancel = new Button(shell, SWT.PUSH);
		btnCancel.setText("Cancel");
		lData = new FormData ();
		lData.width = 60;
		lData.right = new FormAttachment (100, 0);
		lData.bottom = new FormAttachment (100, 0);
		btnCancel.setLayoutData (lData);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				display.close();
				Starter.startMeetingCostClock(false);
			}
		});
		Button btnOk = new Button(shell, SWT.PUSH);
		btnOk.setText("   OK   ");
		btnOk.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				labelMessage.setText("");
				if (isSet(valueNoOfPart.getText())) {
					try {
						settings.setNoOfParticipants(Integer.parseInt(valueNoOfPart.getText()));
					} catch (NumberFormatException ex) {
						labelMessage.setText("Number of participants must be an number.");
						return;
					}
				} else {
					labelMessage.setText("Number of participants has to be set.");
					return;
				}
				
				if (isSet(valueRate.getText())) {
					try {
						settings.setRate(Integer.parseInt(valueRate.getText()));
					} catch (NumberFormatException ex) {
						labelMessage.setText("Hourly rate must be an number.");
						return;
					}
				} else {
					labelMessage.setText("Hourly rate has to be set.");
					return;
				}

				int lIndex = valueLocale.getSelectionIndex();
				settings.setLocale(locales[lIndex]);
				
				settings.setAlwaysOnTop(checkOnTop.getSelection());
				
				settings.save();
				display.close();
				Starter.startMeetingCostClock(true);
			}
		});
		lData = new FormData ();
		lData.width = 60;
		lData.right = new FormAttachment (btnCancel, 0, SWT.DEFAULT);
		lData.bottom = new FormAttachment (100, 0);
		btnOk.setLayoutData (lData);

		
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	boolean isSet(String text) {
		return ((text != null) && (text.length() > 0));
	}

	
	
	public class LocaleComparator implements Comparator<Locale> {

		@Override
		public int compare(Locale o1, Locale o2) {
			return o1.getDisplayName().compareTo(o2.getDisplayName());
		}
		
	}


}
